<?php
namespace Act\Os;
use Act\Os\Backend;
use Lib\Page;
use Lib\Common;
class Region extends Backend{
	private $view_types = ['province'=>'市','city'=>'县','district'=>''];
	private $types = ['province'=>'省份','city'=>'城市','district'=>'区县'];

	public function doGet(){		
		$fid = get('fid',0,'absint');
		$type = get('type','','trim,htmlspecialchars');
		$this->checkParam($fid);
		$this->checkParam($type);

		$query_table = [
			'province'=>'city',
			'city'=>'district',
		];

		if(!in_array($type, array_keys($query_table))){
			return $this->error('非法操作',referer());
		}

		$type = $query_table[$type];

		$mod = model('#'.ucwords($type));
		$count = $mod->searchCount('',$fid);
		$page = new Page($count,self::PAGE_SIZE);
		$regions = $mod->search('',$fid,$page->offset,$page->size);
		$this->display('os/regions:index.php',[
			'regions'=>$regions,
			'type'=>$type,
			'page_html'=>$page->show(),
			'view_type'=>$this->view_types[$type],
			'admin_action_confirm'=>Common::renderJsConfirm('icon-remove'),
		]);
	}

	public function doAdd(){
		if(is_post()){
			$levels = [
			'province'=>post('province','','trim,htmlspecialchars'),
			'city'=>post('city','','trim,htmlspecialchars'),
			];
			$parent_sn = 0;
			$type = 'province';
			$values = array_values($levels);
			$keys = array_keys($levels);
			$count = count($values)-1;
			while($count>=0){
				if(($parent_sn = $values[$count]) && ($type = $keys[$count])){
					break;
				}
				$count--;
			}
			if(!in_array($type, $keys)){
				$this->alert('error','非法操作');
				goto end;
			}
			$type = ['province'=>'city','city'=>'district'][$type];

			$data = [
				'name'=>post('name','','trim,htmlspecialchars'),
				'sn'=>post('sn','','trim,htmlspecialchars'),
			];
			if (empty($data['name']) || empty($data['sn'])){
				$this->alert('error','缺少必要的输入值');
				goto end;
			}

			if($parent_sn && $type!='province'){
				if(empty($parent_sn)){
					$this->alert('error','需要选择上级');
					goto end;
				}
				$data['father'] = $parent_sn;
			}

			$u_type = ucwords($type);
			$mod = model('#'.$u_type);
			//编码是否存在
			if (($method = 'get'.$u_type.'BySn') && $mod->$method($data['sn'])) {
				$this->alert('error','地区编码不能重复');
				goto end;
			}
			$method = 'add'.$u_type;
			
			$id = $mod->$method($data);
			if(!empty($id)){
				return $this->success('添加地区数据成功',referer());
			}else{
				$this->alert('error',$mod->getError());
				goto end;
			}
		}

		end:

		$province_mod = model('#Province');
		$this->display('form.php',[
			'level'=>2,
			'parent_options'=>$province_mod->getOption(''),
		]);
	}

	public function doUpdate(){
		$id = get('id',0,'absint');
		$type = get('type','','trim,htmlspecialchars');
		$this->checkParam($id);
		$this->checkParam($type);
		if(!in_array($type,array_keys($this->types))){
			return $this->error('非法操作',referer());
		}
		$u_type = ucwords($type);
		$mod = model('#'.$u_type);
		$method = 'get'.$u_type.'ById';
		$region = $mod->$method($id);
		if(empty($region)){
			$this->error('不存在该'.$this->types[$type],referer());
		}

		$parent_type = ['province'=>'','city'=>'province','district'=>'city'][$type];
		$parent_type_name = $this->types[$type];
		if(is_post()){
			$parent_id = post($parent_type,'','trim,htmlspecialchars');
			if(!empty($parent_type) && empty($parent_id)){
				$this->alert('error','必须选择上级');
				goto end;
			}
			$data = ['name'=>post('name','','trim,htmlspecialchars'),'sn'=>post('sn',0,'absint')];
			$region = &$data;
			if(empty($data['name']) || empty($data['sn'])){
				$this->alert('error','缺少必要的输入值');
				goto end;
			}
			if(!empty($parent_id)){
				$data['father'] = $parent_id;
			}
			$method = 'update'.$u_type;
			$ret = $mod->$method($id,$data);
			if($ret!==false){
				return $this->success('编辑'.$this->types[$type].'成功',referer());
			}else{
				$this->alert('error',$mod->getError());
				goto end;
			}
		}	
		$_POST = $region;

		end:
		if(!empty($parent_type)){
			$u_type = ucwords($parent_type);
			$parent_mod = model('#'.$u_type);
			$method = 'get'.$u_type.'BySn';
			$_region = $parent_mod->$method($region['father']);
			$this->assign('parent_options',$parent_mod->getOption($_region['sn'],isset($_region['father']) ? $_region['father'] : null));
		}
		$this->display('form.php',[
			'parent_type_name'=>$parent_type_name,
			'view_type'=>$this->view_types[$type],
			'parent_type'=>$parent_type,
			'level'=>['province'=>0,'city'=>1,'district'=>2][$type],
		]);
	}
}